package com.rsc.learningbar.service;

import com.rsc.learningbar.dao.EbbinghausRepository;
import com.rsc.learningbar.dao.StudentMissionRepository;
import com.rsc.learningbar.dao.StudentPlanRepository;
import com.rsc.learningbar.entity.Ebbinghaus;
import com.rsc.learningbar.enumer.MemoryType;
import com.rsc.learningbar.entity.StudentMissionDetail;
import com.rsc.learningbar.entity.StudentPlan;
import com.rsc.learningbar.vo.Plan;
import com.rsc.learningbar.vo.StudentPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class StudentPlanService {

    @Autowired
    StudentPlanRepository planRepository;

    @Autowired
    StudentMissionRepository studentMissionRepository;

    @Autowired
    MemoryType memoryType;

    @Autowired
    DelayQueue delayQueue;

    @Autowired
    EbbinghausRepository ebbinghausRepository;

    public int addStudentPlan(StudentPlanVo infos) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
        String format = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        // 分数计算
        int missionId = 0;
        int current = 0;
        int all = infos.getDetailList().size();
        for (StudentMissionDetail info : infos.getDetailList()) {
            missionId = info.getStudentMissionId();
            if (info.getSolution().equals(info.getAnswer())) {
                current++;
            }
        }

        // 插入第一条
        if (infos.getId() == null) {
            StudentPlan plan = new StudentPlan();
            plan.setUserId(infos.getUserId());
            plan.setStudentMissionId(missionId);
            plan.setMemoryType(0);
            plan.setPoint(String.valueOf(Math.round(Float.valueOf(current) / Float.valueOf(all) * 100)));
            plan.setRemindStatus("1");
            plan.setFinishTime(format);
            plan.setRemindTime(format);
            plan.setRemindedTime(format);
            plan.setCreateTime(format);
            planRepository.save(plan);

            // 查询用户记忆曲线
            Ebbinghaus e = new Ebbinghaus();
            e.setUserId(infos.getUserId());
            Example<Ebbinghaus> example = Example.of(e);
            List<Ebbinghaus> list = ebbinghausRepository.findAll(example, Sort.by(Sort.Direction.ASC, "type"));

            for (Ebbinghaus ebbinghaus : list) {
                // 插入提醒数据
                StudentPlan todoPlan = new StudentPlan();
                todoPlan.setUserId(infos.getUserId());
                todoPlan.setStudentMissionId(missionId);
                todoPlan.setMemoryType(ebbinghaus.getType());
                todoPlan.setRemindStatus("0");
                todoPlan.setCreateTime(format);

                // 计算提醒时间
                int seconds = ebbinghaus.getEbbinghaus();
                String remind = localDateTime.plusSeconds(seconds).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

                todoPlan.setRemindTime(remind);
                // 插入计划
                int id = planRepository.save(todoPlan).getId();

                // 放入延迟队列
                StudentPlanVo info = new StudentPlanVo();
                info.setId(id);
                info.setUserId(infos.getUserId());
                info.setStudentMissionId(missionId);
                info.setStudentMissionName(studentMissionRepository.findById(missionId).get().getStudentMissionName());

                Plan p = new Plan(info, seconds, TimeUnit.SECONDS);
                delayQueue.put(p);
            }

        } else {
            // 根据用户名查询之前数据
            StudentPlan befPlans = planRepository.findById(infos.getId()).get();

            StudentPlan plan = new StudentPlan();
            plan.setId(befPlans.getId());
            plan.setUserId(infos.getUserId());
            plan.setStudentMissionId(missionId);
            plan.setMemoryType(befPlans.getMemoryType());
            plan.setPoint(String.valueOf(Math.round(Float.valueOf(current) / Float.valueOf(all) * 100)));
            plan.setRemindStatus("1");
            plan.setFinishTime(format);
            plan.setRemindTime(befPlans.getRemindTime());
            plan.setRemindedTime(format);
            plan.setCreateTime(befPlans.getCreateTime());
            planRepository.save(plan);
        }


        return 1;
    }

    public StudentPlanVo getStudentPlan(StudentPlanVo infos) {
        List<StudentPlan> plan = planRepository.findStudentPlan(infos.getUserId(), infos.getDate());
        infos.setPlans(plan);
        return infos;
    }

    public Map<String, List<StudentPlan>> getStudentResult(StudentPlanVo infos) {
        List<StudentPlan> plan = planRepository.findStudentResult(infos.getUserId());
        Map<String, List<StudentPlan>> result = plan.stream().collect(Collectors.groupingBy(StudentPlan::getStudentMissionName));
        return result;
    }
}
